package com.welson.db.cryptor.wrapper;

import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.welson.db.cryptor.util.EncryptHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 支持加密字段的QueryWrapper
 * @author welson
 */
public class EncryptLambdaUpdateWrapper<T> extends AbstractEncryptLambdaWrapper<T, EncryptLambdaUpdateWrapper<T>> implements Update<EncryptLambdaUpdateWrapper<T>, SFunction<T, ?>> {

    /**
     * SQL 更新字段内容，例如：name='1', age=2
     */
    private final List<String> sqlSet;

    public EncryptLambdaUpdateWrapper() {
        super.setEntity(null);
        super.initNeed();
        this.sqlSet = new ArrayList<>();
    }

    EncryptLambdaUpdateWrapper(T entity, Class<T> entityClass, List<String> sqlSet, AtomicInteger paramNameSeq,
                        Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments, SharedString paramAlias,
                        SharedString lastSql, SharedString sqlComment, SharedString sqlFirst) {
        super.setEntity(entity);
        super.setEntityClass(entityClass);
        this.sqlSet = sqlSet;
        this.paramNameSeq = paramNameSeq;
        this.paramNameValuePairs = paramNameValuePairs;
        this.expression = mergeSegments;
        this.paramAlias = paramAlias;
        this.lastSql = lastSql;
        this.sqlComment = sqlComment;
        this.sqlFirst = sqlFirst;
    }

    @Override
    protected EncryptLambdaUpdateWrapper<T> instance() {
        return new EncryptLambdaUpdateWrapper<>();
    }

    public EncryptLambdaUpdateWrapper<T> encryptSet(SFunction<T, ?> column, Object val) {
        return encryptSet(true, column, val, null);
    }

    public EncryptLambdaUpdateWrapper<T> encryptSet(boolean condition, SFunction<T, ?> column, Object val) {
        return encryptSet(condition, column, val, null);
    }

    public EncryptLambdaUpdateWrapper<T> encryptSet(SFunction<T, ?> column, Object val, String mapping) {
        return encryptSet(true, column, val, mapping);
    }

    public EncryptLambdaUpdateWrapper<T> encryptSet(boolean condition, SFunction<T, ?> column, Object val, String mapping) {
        return set( condition, column, EncryptHandler.encrypt(val), mapping);
    }

    @Override
    public EncryptLambdaUpdateWrapper<T> set(boolean condition, SFunction<T, ?> column, Object val, String mapping) {
        return maybeDo(condition, () -> {
            String sql = formatParam(mapping, val);
            sqlSet.add(columnToString(column) + Constants.EQUALS + sql);
        });
    }

    @Override
    public EncryptLambdaUpdateWrapper<T> setSql(boolean condition, String sql) {
        if (condition && StringUtils.isNotBlank(sql)) {
            sqlSet.add(sql);
        }
        return typedThis;
    }

    @Override
    public String getSqlSet() {
        if (CollectionUtils.isEmpty(sqlSet)) {
            return null;
        }
        return String.join(Constants.COMMA, sqlSet);
    }
}